package com.ozo.easy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ozo.easy.common.exception.ServiceException;
import com.ozo.easy.common.model.BaseBatchDTO;
import com.ozo.easy.common.model.BasePage;
import com.ozo.easy.common.model.PageResult;
import com.ozo.easy.common.util.SortUtil;
import com.ozo.easy.entity.SysDictData;
import com.ozo.easy.exception.ServiceExceptionEnum;
import com.ozo.easy.mapper.SysDictDataMapper;
import com.ozo.easy.pojo.dto.SysDictDataSaveDTO;
import com.ozo.easy.pojo.dto.SysDictDataQueryDTO;
import com.ozo.easy.pojo.vo.SysDictDataVO;
import com.ozo.easy.service.ISysDictDataService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 数据字典 服务实现类
 *
 * @author ozo
 * @version 1.0
 * @date 2025-01-21 10:37:26
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SysDictDataSaveDTO saveDTO) {
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictData::getDictValue, saveDTO.getDictValue())
                .eq(SysDictData::getDictTypeId, saveDTO.getDictTypeId());
        Long count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new ServiceException(ServiceExceptionEnum.DICT_VALUE_CODE_EXISTS);
        }

        SysDictData sysDictData = BeanUtil.copyProperties(saveDTO, SysDictData.class);
        baseMapper.insert(sysDictData);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void remove(BaseBatchDTO<Long> batchDTO) {
        baseMapper.deleteBatchIds(batchDTO.getIdList());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SysDictDataSaveDTO saveDTO) {
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictData::getDictValue, saveDTO.getDictValue())
                .eq(SysDictData::getDictTypeId, saveDTO.getDictTypeId())
                .ne(SysDictData::getId, saveDTO.getId());
        Long count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new ServiceException(ServiceExceptionEnum.DICT_VALUE_CODE_EXISTS);
        }

        SysDictData sysDictData = BeanUtil.copyProperties(saveDTO, SysDictData.class);
        baseMapper.updateById(sysDictData);
    }

    @Override
    public PageResult<SysDictDataVO> page(BasePage basePage, SysDictDataQueryDTO queryDTO) {
        Integer pageNum = basePage.getPageNum();
        Integer pageSize = basePage.getPageSize();

        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();
        // 排序
        String order = queryDTO.getOrder();
        if (StrUtil.isBlank(order)) {
            queryWrapper.lambda().orderByAsc(SysDictData::getOrderNum);
        } else {
            SortUtil.setSortCondition(queryWrapper, queryDTO.getProp(), order);
        }
        // 条件
        queryWrapper.lambda()
                .eq(Objects.nonNull(queryDTO.getDictTypeId()), SysDictData::getDictTypeId, queryDTO.getDictTypeId())
                .like(StrUtil.isNotBlank(queryDTO.getDictItem()), SysDictData::getDictItem, queryDTO.getDictItem())
                .like(StrUtil.isNotBlank(queryDTO.getDictValue()), SysDictData::getDictValue, queryDTO.getDictValue());
        Page<SysDictData> page = baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);

        PageResult<SysDictDataVO> pageResult = new PageResult<>();
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        pageResult.setPage(page.getPages());
        pageResult.setTotal(page.getTotal());
        List<SysDictData> recordList = page.getRecords();
        List<SysDictDataVO> recordVOList = BeanUtil.copyToList(recordList, SysDictDataVO.class);
        pageResult.setResults(recordVOList);
        return pageResult;
    }

    @Override
    public SysDictDataVO detail(Long id) {
        SysDictData sysDictData = baseMapper.selectById(id);
        if (Objects.isNull(sysDictData)) {
            throw new ServiceException(ServiceExceptionEnum.DATA_NOT_EXISTS);
        }
        return BeanUtil.copyProperties(sysDictData, SysDictDataVO.class);
    }

}
